$(function(){

  window.routerSock = true;
  var routehandle = function(data){
    return function(){
    	if(window.routerSock){
    		window.routerSock = false;
    		var commonControlFunc = function (){
        		if(navigator.userAgent.indexOf("MSIE 8")>0){
        	          document.title = window.platform+'-' + data.title;
        	        }else{
        	          $('title').html( window.platform+'-' + data.title);
        	        }

        	        //----------------------------高亮定位 start --------------------------
        	        //顶部导航 高亮定位
        	        $('.header ul.nav>li>a').removeClass('active');
        	        if($('a[href$="'+location.hash+'"]').parents('.nav').length == 1){
        	          if($('a[href$="'+location.hash+'"]').parents('.subnav').length ==1 ){
        	              $('a[href$="'+location.hash+'"]').parents('li').find('>a').addClass('active');
        	          }else{
        	              $('a[href$="'+location.hash+'"]').addClass('active');
        	          }
        	        }

        	        //特殊处理
        	        if(location.hash === '#proxy/vip/add'){ //添加会员
        	          $('a[href$="proxy/vip"]').parents('li').find('>a').addClass('active');  
        	        }
        	        if(location.hash === '#user/goAddUserBank'){  //添加绑定银行
        	           $('a[href$="user/info"]').parents('li').find('>a').addClass('active');
        	        }
        	        if(location.hash.indexOf('user/goModUserBank') > 0){  //修改绑定银行
        	          $('a[href$="user/info"]').parents('li').find('>a').addClass('active');
        	        }
        	        if(location.hash.indexOf('notice/detail') > 0){     //公告详情
        	          $('a[href$="notice/info"]').parents('li').find('>a').addClass('active');
        	        }

        	        //左侧导航栏 高亮定位
        	        $('#left-menu dd').removeClass('over');
        	        if($('a[href$="'+location.hash+'"]').parents('#left-menu').length == 1){
        	          var _clickTag = $('a[href$="'+location.hash+'"]').
        	            parent()[0].tagName.toUpperCase();
        	          if(_clickTag == 'DD'){
        	            $('a[href$="'+location.hash+'"]').parent()[0].onmouseout = function (){};
        	            $('a[href$="'+location.hash+'"]').parent().addClass('over');
        	            $('#left-menu dd').each(function (i , ele){
        	                if($('a[href$="'+location.hash+'"]').attr('href') !== 
        	                  $(ele).find('>a').attr('href')){
        	                  $(ele)[0].onmouseout = function (){
        	                    $(this).removeClass('over');
        	                  }
        	                }
        	            });
        	          }
        	        }
        	      //----------------------------高亮定位 end --------------------------
                	//清空定时器
                  if($('#countdown_dashboard')[0]){
                    $('#countdown_dashboard').stopCountDown();
                  }
                  if($('#lottery-data')[0]){
                    clearInterval($.data($('#lottery-data')[0], 'open_timer'));
                    clearInterval($.data($('#lottery-data')[0], 'wait_timer'));
                  }
                  if($('.kj-hao')[0]){
                    clearTimeout($.data($('.kj-hao')[0], 'kaijiang_timer'));
                  }
                  if(window.Pager && window.Pager.data){
                    window.Pager.data = null;
                  }
                  if(data.url.indexOf('?') > 0){
                	  data.url = data.url + '&r=' + new Date().getTime();
                  }else{
                	  data.url = data.url + '?r=' + new Date().getTime();
                  }
                  $('#content-lottery').load(data.url,function (response,status,xhr){
                	  window.routerSock = true;
                  });
    		};
    		
    		//如果是时时彩的hash，就延迟800毫秒执行
    		/^#\/lottery/.test(location.hash) && (function (){
        		window.setTimeout(function (){
        			commonControlFunc();
        		},800);
    		})();
    		!/^#\/lottery/.test(location.hash) && (function (){
    			commonControlFunc();
    		})();
    	}
    }
  }
    var routes = {
      '/home': routehandle({
        title : '首页',
        url: basePath + "content/home"
      }),
      '/message/info' : routehandle({
        title : '消息',
        url: basePath + "content/message/info"
      }),
      '/help/info' : routehandle({
        title : '帮助中心',
        url: basePath + "content/help/info"
      }),
      '/bank/recharge': routehandle({
        title : '充值',
        url: basePath + "content/bank/recharge"
      }),
      '/history/info' : routehandle({
        title : '游戏记录',
        url: basePath + "content/history/info"
      }),
      '/bank/tocash': routehandle({
        title : '提现',
        url: basePath + "content/bank/tocash"
      }),
      '/user/coin': routehandle({
        title : '帐变记录',
        url: basePath + "content/user/coin"
      }),
      '/user/rechargeLog': routehandle({
        title : '会员充值',
        url: basePath + "content/user/rechargeLog"
      }),
      '/user/toCashLog': routehandle({
        title : '会员提现',
        url: basePath + "content/user/toCashLog"
      }),
      '/notice/info': routehandle({
        title : '系统公告',
        url: basePath + "content/notice/info"
      }),
      '/lottery': {
          '/?([^\/]*)\/([^\/]*)/?': function (type, name) {
            routehandle({
              title : '投彩',
                url: basePath + "content/lottery/"+type+"/"+name
              })();
          }
      },
      '/proxy/vip' : routehandle({
        title : '会员管理',
        url : basePath + 'content/proxy/vip'
      }),
      '/user/passwd' : routehandle({
        title : '密码管理',
        url : basePath + 'user/content/passwd'
      }),
      '/user/coinPasswd' : routehandle({
          title : '资金密码',
          url : basePath + 'user/content/coinPasswd'
        }), 
      '/notice/detail/:id': function (_id){
        routehandle({
            title : '公告详情',
            url : basePath + 'notice/detail/' + _id
          })();
      },
      '/proxy/vip/add' : routehandle({
        title : '添加会员',
        url : basePath + 'content/proxy/vip/add'
      }),
      '/team/coinall' : routehandle({
        title : '团队统计',
        url : basePath + 'content/team/coinall'
      }),
      '/bill/info' : routehandle({
        title : '盈亏报表',
        url : basePath + 'content/bill/info'
      }),
      '/team/report' : routehandle({
        title : '团队盈亏报表',
        url : basePath + 'content/team/report'
      }), 
      '/team/cashRecord' : routehandle({
        title : '团队提现',
        url : basePath + 'content/team/cashRecord'
      }),
      '/team/gameRecord' : routehandle({
        title : '团队游戏记录',
        url: basePath + "content/team/gameRecord"
      }),
      '/team/coin' : routehandle({
        title : '团队帐变记录',
        url: basePath + "content/team/coin"
      }),
      '/user/info' : routehandle({
        title : '个人资料',
        url: basePath + "user/content/info"
      }),
      '/user/banklist' : routehandle({
          title : '银行管理',
          url: basePath + "user/content/banklist"
       }),  
      '/user/goAddUserBank' : routehandle({
        title : '添加银行操作',
        url: basePath + "user/content/goAddUserBank"
      }),
      '/user/goModUserBank/:id': function (id){
        routehandle({
            title : '修改绑定银行',
            url : basePath + 'user/content/goModUserBank/' + id
          })();
      },
      '/user/opUserBank' : routehandle({
        title : '操作绑定银行',
        url: basePath + "user/content/opUserBank"
      }),
      '/notice/announcement' : routehandle({
        title : '最新活动',
        url: basePath + "content/notice/announcement"
      }), 
      '/user/setDefUserBank/:id': function (id){
        routehandle({
            title : '设置默认绑定银行',
            url : basePath + 'user/content/setDefUserBank/' + id
          })();
      },
      '/user/onDefUserBank/:id': function (id){
          routehandle({
              title : '取消默认绑定银行',
              url : basePath + 'user/content/onDefUserBank/' + id
            })();
        }
    };
    
    var router = Router(routes);
    router.init();
});